package com.touch.jgloo.core;

import java.awt.Color;
import javax.media.opengl.GL;
import com.touch.jgloo.core.JGLPoint;
/**
 * JGLLine represents a Line drawn onto the screen by using JGLOO/OWT.
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r461
 * @see com/touch/Licence
 */
public class JGLLine extends JGLObject
{
	/**
	 * Constructs a JGLLine using the points (0,0,0) and (0,0,0).
	 * @since r461
	 */
	public JGLLine()
	{
		//create a polygon with 2 points
		final JGLPolygon polygon = new JGLPolygon(2);
		
		//set the render state of the line
		polygon.renderState = GL.GL_LINE;
		//add the polygon to the object
		this.addPolygon(polygon);
		//build the line
		this.setLine(0, 0, 0, 0, 0, 0);
	}
	/**
	 * Constructs a JGLLine using defined points
	 * @param x1 First x-coordinate
	 * @param y1 First y-coordinate
	 * @param z1 First z-coordinate
	 * @param x2 Second x-coordinate
	 * @param y2 Second y-coordinate
	 * @param z2 Second z-coordinate
	 * @since r461
	 */
	public JGLLine(float x1, float y1, float z1, float x2, float y2, float z2)
	{
		//create a new polygon with 2 points
		final JGLPolygon polygon = new JGLPolygon(2);
		
		//set the render  state of the line
		polygon.renderState = GL.GL_LINE;
		//add the polygon to the object
		this.addPolygon(new JGLPolygon(2));
		//build the line
		setLine(x1, y1, z1, x2, y2, z2);
	}
	/**
	 * Sets the line by using a list of points
	 * @param vert Points. Note: There must be 6 verts (vert0 to vert5).
	 * @since r461
	 */
	public synchronized void setLine(float... vert)
	{
		//get the polygon
		final JGLPolygon polygon = this.getPolygon(0);
		
		//set point1
		polygon.setPoint(0, new JGLPoint(vert[0], vert[1], vert[2]));
		//set point2
		polygon.setPoint(1, new JGLPoint(vert[3], vert[4], vert[5]));	
	}
	/**
	 * Sets the color of the line. There must be 2 Colors in the list
	 * @param color Color to use
	 * @since r461
	 */
	public void setColor(final Color... color)
	{
		//get the polygon
		final JGLPolygon polygon = this.getPolygon(0);
		
		//set the color of point 1
		polygon.setColor(0, color[0]);
		//set the color of point 2
		polygon.setColor(1, color[1]);
	}
	/**
	 * Sets the color of the JGLLine using only a single color
	 * @param color Color to use
	 * @since r461
	 */
	public void setColor(final Color color)
	{
		//get the polygon
		final JGLPolygon polygon = this.getPolygon(0);
		
		//set the color of the first point
		polygon.setColor(0, color);
		//set the color of the second point
		polygon.setColor(1, color);
	}
	/**
	 * Sets the color of a point defined by its Index
	 * @param vertex Index of the point. Value must be [0, 1].
	 * @param color Color to use.
	 * @since r461
	 */
	public void setColor(int vertex, Color color)
	{
		//get the polygon
		final JGLPolygon polygon = this.getPolygon(0);
		//set the color
		polygon.setColor(vertex, color);
	}
	/**
	 * Returns the color of a point
	 * @param vertex Index of the point
	 * @return Color of the point
	 * @since r461
	 */
	public Color getColor(int vertex)
	{
		//returns the color value
		return this.getPolygon(0).getColor(vertex);
	}
	/**
	 * Overrides the render method of JGLObject so that it works with lines.
	 * Note: You cannot texture a line.
	 * @since r461
	 */
	public void render()
	{
		//begin the draw instance
		GL_INSTANCE.glBegin(GL.GL_LINE);
			//draw the polygon
			this.getPolygon(0).render();
		//end the draw instance
		GL_INSTANCE.glEnd();
	}
	/**
	 * Overrides the toString method to display information about the JGLLine.
	 * @return Returns a String describing the JGLLine.
	 * @since r461
	 */
	public String toString()
	{
		//get the polygon
		final JGLPolygon polygon = this.getPolygon(0);
		//return the Structured string.
		return "JGLLine: " + 
			polygon.getPoint(0).toString() + ", " + 
			polygon.getPoint(1).toString();
	}
}
